<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>${empty dept ? "新增": "修改"}部门</title>
    <base href="${pageContext.request.contextPath}/">
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/common.css">
</head>
<body>
<div style="padding: 15px;">
    <form class="layui-form layui-form-pane" action="">
        <input type="hidden" name="id" value="${dept.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="请输入部门名称" autocomplete="off"
                       class="layui-input" value="${dept.name}">
            </div>
        </div>
        <div class="layui-form-item text-center">
            <button class="layui-btn" lay-submit lay-filter="dept-form">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>

    <script src="layui/layui.js"></script>
    <script>
        layui.use(['element', 'form', 'layer'], function () {
            var element = layui.element,
                form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            //注意：parent 是 JS 自带的全局对象，可用于操作父页面
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

            form.on('submit(dept-form)', function (data) {
                console.log(data.field);
                $.ajax({
                    url: "${pageContext.request.contextPath}/admin/dept/detail",
                    method: "POST",
                    data: data.field,
                    success: function (data) {
                        console.log(data);
                        if (data.code == 200) {
                            // 父窗口提示
                            parent.layer.msg("保存" + data.data.name + "成功！");
                            // 重载列表页表格
                            parent.layui.$("#dept-query").click();
                            // 关闭窗口
                            parent.layer.close(index);
                        }
                    },
                    dataType: "json"
                });
                return false;
            });
        });
    </script>
</body>
</html>
